package com.edu.eduservice.controller;


import com.edu.commonutils.R;
import com.edu.eduservice.domain.EduChapter;
import com.edu.eduservice.service.EduChapterService;
import com.edu.servicebase.exceptionhandler.MyException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author ljw
 * @since 2022-09-16
 */
@Api(description = "章节接口")
//@CrossOrigin
@RestController
@RequestMapping("/eduservice/educhapter")
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    //根据课程找到章节和小节
    @ApiOperation(value = "根据课程编号找到章节和小节")
    @GetMapping("/{courseid}")
    @ResponseBody
    public R getChapter(@PathVariable String courseid){
        R r= this.eduChapterService.getChapterService(courseid);
        return r;
    }

    //添加章节接口
    @ApiOperation(value = "添加章节接口")
    @PostMapping("/save")
    @ResponseBody
    public R save(@RequestBody EduChapter eduChapter){
        boolean boo = this.eduChapterService.save(eduChapter);
        R r=null;
        //判断是否添加成功
        if(boo) {
           r=R.ok();
        }else {
            r=R.error();
        }
        return r;
    }

    //根据编号查询返回
    @ApiOperation(value = "根据编号查询")
    @GetMapping("/byid/{id}")
    @ResponseBody
    public R getByid(@PathVariable String id){
        //查询出来
        EduChapter chapter = this.eduChapterService.getById(id);
        //封装数据
        R r = R.ok().data("chapter", chapter);
        return  r;
    }

    //修改章节
    @ApiOperation(value = "修改章节")
    @PostMapping("/edit")
    @ResponseBody
    public R update(@RequestBody EduChapter eduChapter){
        boolean boo = this.eduChapterService.updateById(eduChapter);
        R r=null;
        //判断是否修改成功
        if(boo) {
            r=R.ok();
        }else {
            r=R.error();
        }
        return r;
    }

    //删除章节
    @ApiOperation(value = "删除章节")
    @DeleteMapping("/del/{id}")
    @ResponseBody
    public R delChapter(@PathVariable String  id) throws MyException {
        boolean boo = this.eduChapterService.delChapterService(id);
        R r=null;
        //判断是否添加成功
        if(boo) {
            r=R.ok();
        }else {
            r=R.error().message("未删除小节或其他未知错误....");
        }
        return r;
    }



}

